﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Server.Models;
using Server.Share;

namespace Server.Controllers.Manage
{
    [RoutePrefix("51API/Manager")]
    public class ManagerLoginController : ApiController
    {
        private MShare comm = new MShare();
        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <returns></returns>
        [Route("ManagerLogin")]
        public IHttpActionResult PostManagerLogin(ManagerModel managerModel)
        {
            try
            {
                ManageLoginModel ml = comm.managerCtrl.ManagerLogin(managerModel);
                RetModel ret = new RetModel
                {
                    Data = ml,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Manage001", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 管理员退出
        /// </summary>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("ManagerLogout")]
        public IHttpActionResult PostManagerLogout([FromBody]string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                comm.CheckAuth(comm.manager.id, "");
                comm.managerCtrl.ManagerLogout(Token);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Manage002", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 管理员添加
        /// </summary>
        /// <returns></returns>
        [Route("ManagerAdd")]
        public IHttpActionResult PostManagerAdd(ManagerModel managerModel, string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                comm.CheckAuth(comm.manager.id, "Manager001");
                comm.managerCtrl.ManagerAdd(managerModel);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Manage002", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="managerModel"></param>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("PassEdit")]
        public IHttpActionResult PostPassEdit(ManagerModel managerModel, string OldPass, string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                //comm.CheckAuth(comm.manager.id, "");
                comm.managerCtrl.PassEdit(managerModel, OldPass);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Manage002", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 个人信息修改
        /// </summary>
        /// <returns></returns>
        [Route("ManagerEdit")]
        public IHttpActionResult PostManagerEdit(ManagerModel managerModel, string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                //comm.CheckAuth(comm.manager.id, "");
                comm.managerCtrl.ManagerEdit(managerModel);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Manage003", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 管理员启用/禁用
        /// </summary>
        /// <returns></returns>
        [Route("ManagerState")]
        public IHttpActionResult PostManagerState(int id, string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                comm.CheckAuth(comm.manager.id, "Manager002");
                comm.managerCtrl.ManagerState(id);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Manage004", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 通过ID获取管理员信息
        /// </summary>
        /// <param name="id">管理员ID</param>
        /// <param name="Token">令牌</param>
        /// <returns></returns>
        [Route("GetManagerByID")]
        public IHttpActionResult PostGetManagerByID(int id, string Token)
        {
            try
            {
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Manage005", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 获取管理员列表
        /// </summary>
        /// <returns></returns>
        [Route("GetManagerList")]
        public IHttpActionResult PostGetManagerList(string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                //comm.CheckAuth(comm.manager.id, "");
                ManagerModel[] manager = comm.managerCtrl.GetManagerList();
                RetModel ret = new RetModel
                {
                    Data = manager,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Manage006", ErrInfo = ex.Message, Data = null });
            }
        }
    }
}
